
<div id="comments">

<%include file="list_frag.html"/>

<%! from mtp.lib.permissions import has_perm %>

% if has_perm('comment', 'create'):
    <div class="comment_options">
        <ul>
        <li><a href="${h.url_for('page_new_comment', page_id=c.page.urlTitle())}" class="add_comment">Add Comment</a></li>
        </ul>
    </div>
% endif

<script language="javascript">
<!--

var comments = {
    get_comment_form: function() {
        var parent = $(this)[0].parentNode.parentNode;
        $('#comments a.add_comment').hide();
        $.get($(this)[0].href, {fragment:'1'}, 
            function(xml) {
                $(parent).after(xml);
                $('#comments form').submit(comments.submit_comment_form);
            }
        );
        return false;
    }
    
    /*
    flatten: function() {
        var self = $(this)[0];
        var children = $('.comment', $(this));
        console.log($(this), children);
        if( children ) {
            var link = $('<li><a class="expand_comment">Expand<a></li>');
            console.log('ul chilren', $(this).children('comment_options ul') );
            $('.expand_comment', $(this)).click(function(){ alert('bo'); });
        }
    }
    */
};

comments.submit_comment_form = function() {
    var data = $(this).serialize();
    $.post(this.action, data,
        function(xml) { 
            $('#comments .comment_list').html(xml);
            $('#comments form').remove();
            $('#comments a.add_comment').show();
            comments.add_extras();
        }
    );
    return false;
};

comments.add_extras = function() {
    $('#comments a.add_comment').click(comments.get_comment_form);
    //$('.comment_list > .comment').each(comments.flatten);
    $('#comments a.delete_comment').click(function() { 
        return confirm('Are you sure you want to delete this comment?');
    });
};

$(document).ready(function() {
    comments.add_extras()
});
-->
</script>

</div>
